package view.dlct.ngonngu.backing;

import java.util.ArrayList;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import javax.faces.event.PhaseEvent;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichDialog;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.jbo.NameValuePairs;
import oracle.jbo.Row;

import view.common.Utils;

public class DmNgonNguBean {
    private RichPopup popupUpdate;
    private RichPopup popupDelete;
    private RichDialog dialogUpdate;

    public DmNgonNguBean() {
    }

    public void beforePhase(PhaseEvent phaseEvent) {
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            resetForm();
            rollback(null);
        }
    }

    private void resetForm() {
        Utils.search("DlctNgonNguView1Iterator", new ArrayList());
    }

    public void showPopupThemMoi(ActionEvent actionEvent) {
        dialogUpdate.setTitle((String)Utils.getElExpression("#{adfBundle['view.ViewControllerBundle'].THEM_MOI}"));
        dialogUpdate.setAffirmativeTextAndAccessKey((String)Utils.getElExpression("#{adfBundle['view.ViewControllerBundle'].THEM_MOI}"));
        DCIteratorBinding ngonNguView1Iterator =
            Utils.getIteratorBinding("DlctNgonNguView1Iterator");
        Row row =
            ngonNguView1Iterator.getViewObject().createAndInitRow(new NameValuePairs());
        ngonNguView1Iterator.executeQuery();
        ngonNguView1Iterator.setCurrentRowWithKey(row.getKey().toStringFormat(false));
        Utils.showPopup(popupUpdate);
    }

    public void showPopupSua(ActionEvent actionEvent) {
        DCIteratorBinding ngonNguView1Iterator =
            Utils.getIteratorBinding("DlctNgonNguView1Iterator");
        if(ngonNguView1Iterator.getCurrentRow()!=null){
        dialogUpdate.setTitle((String)Utils.getElExpression("#{adfBundle['view.ViewControllerBundle'].CAP_NHAT}"));
        dialogUpdate.setAffirmativeTextAndAccessKey((String)Utils.getElExpression("#{adfBundle['view.ViewControllerBundle'].CAP_NHAT}"));
        Utils.showPopup(popupUpdate);
        }else{
            String errorString =
                Utils.getResourceBundle("CHON_BAN_GHI_DE_THAO_TAC");
            FacesContext.getCurrentInstance().addMessage(null,
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
        }
    }

    public void showPopupXoa(ActionEvent actionEvent) {
        DCIteratorBinding ngonNguView1Iterator =
            Utils.getIteratorBinding("DlctNgonNguView1Iterator");
        if(ngonNguView1Iterator.getCurrentRow()!=null){
        Utils.showPopup(popupDelete);
        }else{
            String errorString =
                Utils.getResourceBundle("CHON_BAN_GHI_DE_THAO_TAC");
            FacesContext.getCurrentInstance().addMessage(null,
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
        }
    }

    public void rollback(PopupCanceledEvent popupCanceledEvent) {
        DCIteratorBinding ngonNguView1Iterator =
            Utils.getIteratorBinding("DlctNgonNguView1Iterator");
        ngonNguView1Iterator.getDataControl().getApplicationModule().getTransaction().rollback();
        String rowKey = null;
        if (ngonNguView1Iterator.getCurrentRow() != null)
            rowKey =
                    ngonNguView1Iterator.getCurrentRow().getKey().toStringFormat(false);
        ngonNguView1Iterator.executeQuery();
        if (rowKey != null)
            ngonNguView1Iterator.setCurrentRowWithKey(rowKey);
    }

    public void commit(DialogEvent dialogEvent) {
        DCIteratorBinding ngonNguView1Iterator =
            Utils.getIteratorBinding("DlctNgonNguView1Iterator");
        List<String> listTruongTt = new ArrayList();
        listTruongTt.add("Ma");
        listTruongTt.add("Ten");
        for (String tenField : listTruongTt) {
            String value =
                (String)ngonNguView1Iterator.getCurrentRow().getAttribute(tenField);
            if (value != null) {
                ngonNguView1Iterator.getCurrentRow().setAttribute(tenField,
                                                                   value.trim());
            }
        }
        ngonNguView1Iterator.getDataControl().getApplicationModule().getTransaction().commit();
        String rowKey =
            ngonNguView1Iterator.getCurrentRow().getKey().toStringFormat(false);
        ngonNguView1Iterator.executeQuery();
        ngonNguView1Iterator.setCurrentRowWithKey(rowKey);
    }

    public void delete(DialogEvent dialogEvent) {
        try{
        DCIteratorBinding ngonNguView1Iterator =
            Utils.getIteratorBinding("DlctNgonNguView1Iterator");
        ngonNguView1Iterator.removeCurrentRow();
        ngonNguView1Iterator.getDataControl().getApplicationModule().getTransaction().commit();
        ngonNguView1Iterator.executeQuery();
        }catch(Exception e){
            String errorString =
                Utils.getResourceBundle("CON_BAN_GHI_LIEN_QUAN");
            FacesContext.getCurrentInstance().addMessage(dialogEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
            e.printStackTrace();
        }
    }

    public void setPopupUpdate(RichPopup popupUpdate) {
        this.popupUpdate = popupUpdate;
    }

    public RichPopup getPopupUpdate() {
        return popupUpdate;
    }

    public void setPopupDelete(RichPopup popupDelete) {
        this.popupDelete = popupDelete;
    }

    public RichPopup getPopupDelete() {
        return popupDelete;
    }

    public void setDialogUpdate(RichDialog dialogUpdate) {
        this.dialogUpdate = dialogUpdate;
    }

    public RichDialog getDialogUpdate() {
        return dialogUpdate;
    }
}
